1 Loading

1.1 Packages

suppressPackageStartupMessages({
  library(tidyverse)
  library(readxl)
  library(lubridate)
  library(airportr)
})

theme_set(theme_bw(18) +
            theme(legend.position = "bottom"))
# download.file(url = "https://www.bitre.gov.au/sites/default/files/documents/faresforbi_excel_0320.xlsx",
#               destfile = "raw_aus_airfare.xlsx")

raw_data = readxl::read_excel(path = "raw_aus_airfare.xlsx", sheet = 1) %>%
  janitor::clean_names() %>%
  dplyr::mutate(year_month = paste0(year, "_", month) %>% 
                  lubridate::ymd(truncated = 1),
                covid = (year_month >= "2019-11-01") & (year_month <= "2020-04-01")) %>%
  janitor::remove_empty()
## New names:
## * `` -> ...9
## * `` -> ...10
## * `` -> ...11
## * `` -> ...12
## * `` -> ...13
## value for "which" not specified, defaulting to c("rows", "cols")
glimpse(raw_data)
## Rows: 8,701
## Columns: 9
## $ year       <dbl> 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010…
## $ month      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ year_month <date> 2010-01-01, 2010-01-01, 2010-01-01, 2010-01-01, 2010-01-0…
## $ port1      <chr> "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide"…
## $ port2      <chr> "Brisbane", "Canberra", "Darwin", "Gold Coast", "Melbourne…
## $ route      <chr> "Adelaide - Brisbane", "Adelaide - Canberra", "Adelaide - …
## $ value      <dbl> 208.00, 258.00, 358.00, 128.00, 86.00, 288.00, 212.85, 116…
## $ real       <dbl> 255.63025, 317.07983, 439.97899, 157.31092, 105.69328, 353…
## $ covid      <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FA…

2 Extra location data via airportr and ggmap

# library(ggmap)
# 
# location_tbl = tibble(
#   city = c(raw_data$port1, raw_data$port2) %>% unique,
#   geo = purrr::map(city, ~ geocode(paste0(.x, ", Australia"))))
# 
# ## Adding airport location
# location_tbl2 = location_tbl %>% 
#   tidyr::unnest(geo) %>% 
#   dplyr::mutate(airport = purrr::map2(
#     .x = lat, .y = lon, 
#     .f = ~ airports_around(lat = .x, lon = .y, distance = 50) %>% 
#       slice(1))) %>% 
#   tidyr::unnest(airport) %>% 
#   dplyr::select(city, city_lon = lon, city_lat = lat,
#                 airport_name = Name, itat = IATA,
#                 airport_lon = Longitude, airport_lat = Latitude)
#   
# saveRDS(location_tbl2, file = "location_tbl.rds")
location_tbl = readRDS("location_tbl.rds")

3 Plotting airfares

raw_data %>% 
  ggplot(aes(x = factor(month), y = real)) +
  geom_path(aes(group = route)) +
  facet_wrap(~year)

3.1 Melbourne to Sydney

library(directlabels)
raw_data %>% 
  dplyr::filter(route == "Melbourne - Sydney") %>% 
  ggplot(aes(x = factor(month), y = real, colour = factor(year))) +
  geom_line(aes(group = year, size = covid)) +
  geom_dl(aes(label = year), method = "first.qp") +
  scale_size_manual(values = c(1, 3))

4 Mapbox

path_data = raw_data %>% 
  dplyr::filter(year == "2020", month == "4") %>% 
  left_join(location_tbl, by = c("port1" = "city")) %>% 
  left_join(location_tbl, by = c("port2" = "city"), suffix = c("_port1", "_port2")) %>% 
  dplyr::mutate(real2 = real %>% log)


library(mapdeck)
## 
## Attaching package: 'mapdeck'
## The following object is masked from 'package:tibble':
## 
##     add_column
set_token(Sys.getenv("MAPBOX_AUTH"))


mapdeck(token = Sys.getenv("MAPBOX_AUTH"), style = mapdeck_style('dark')) %>%
  add_line(
    data = path_data, 
    origin = c("city_lon_port1", "city_lat_port1"), 
    destination = c("city_lon_port2", "city_lat_port2"), 
    # stroke_from = "port1", 
    # stroke_to = "port2",
    tooltip = "real",
    layer_id = 'arclayer',
    stroke_colour = "real",
    stroke_width = "real2",
    # legend = TRUE
  )
## Registered S3 method overwritten by 'jsonify':
##   method     from    
##   print.json jsonlite
mapdeck(style = mapdeck_style('dark')) %>%
  add_animated_arc(
    data = path_data, 
    origin = c("city_lon_port1", "city_lat_port1"), 
    destination = c("city_lon_port2", "city_lat_port2"), 
    # stroke_from = "port1", 
    # stroke_to = "port2",
    tooltip = "real",
    layer_id = 'arclayer',
    # stroke_colour = "real",
    stroke_width = "real2",
    trail_length = 10,
    legend = TRUE
  )
## animated_arc is an experimental layer and the function may change without warning